import 'package:flutter/material.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

enum LBadgeType {
  dot,
  text,
}

/// 右上角悬浮徽章
class LBadges extends StatefulWidget {
  /// 要显示悬浮徽章的组件
  final Widget child;

  /// 显示的值
  final int? count;

  /// 最大显示的值，超出后显示+
  final int? maxCount;

  /// 类型 决定是否显示文字
  final LBadgeType? type;

  /// 状态 决定按什么颜色显示
  final LUIType? status;

  /// 背景色 优先级高于`status`
  final Color? backgroundColor;

  /// 文字颜色
  final Color? textColor;

  /// 文字字号
  final double? fontSize;

  /// 徽章宽度
  final double? dotWidth;
  const LBadges(
      {required this.child,
      this.count,
      this.maxCount,
      this.type = LBadgeType.text,
      this.status = LUIType.danger,
      this.backgroundColor,
      this.textColor,
      this.fontSize,
      this.dotWidth,
      Key? key})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _LBadges();
}

class _LBadges extends State<LBadges> {
  double top = 0;
  double right = 0;
  @override
  void initState() {
    super.initState();
    top = -((widget.fontSize ?? LStyle.fontSize) + lrpx(8)) / 2;
    right = top;
  }

  handleFrameSize(LPostFrameInfoValue value) {
    setState(() {
      top = -value.hieght / 2;
      right = -(value.width - value.hieght / 2);
    });
  }

  @override
  Widget build(BuildContext context) {
    var useBackgroundColor = widget.backgroundColor ??
        (widget.type == LUIType.primary
            ? LUI.instance.primaryColor
            : widget.type == LUIType.warning
                ? LUI.instance.warningColor
                : widget.type == LUIType.success
                    ? LUI.instance.successColor
                    : LUI.instance.dangerColor);
    var useText = '${widget.count ?? 0}';
    if (widget.maxCount != null && (widget.count ?? 0) > widget.maxCount!) {
      useText = '${widget.maxCount!}+';
    }
    var useTextColor = widget.textColor ?? Colors.white;

    var useDotWidth =
        (widget.dotWidth ?? widget.fontSize ?? LStyle.fontSize) + lrpx(6);
    if (widget.type == LBadgeType.dot) {
      useDotWidth = widget.dotWidth ?? lrpx(10);
    }
    return Stack(
      clipBehavior: Clip.none,
      children: [
        widget.child,
        if ((widget.count ?? 0) > 0)
          Positioned(
            top: top,
            right: right,
            child: LPostFrameInfo(
              callback: handleFrameSize,
              child: LBox(
                alignment: Alignment.center,
                border: const [true],
                borderSide: BorderSide(
                    color: Colors.white,
                    width: widget.type == LBadgeType.dot ? lrpx(1) : lrpx(1)),
                padding: widget.type == LBadgeType.dot ? [0] : [0, lrpx(4)],
                backgroundColor: useBackgroundColor,
                width: widget.type == LBadgeType.dot ? useDotWidth : null,
                height: useDotWidth,
                borderRadius: [useDotWidth / 2],
                child: widget.type == LBadgeType.dot
                    ? null
                    : LText(
                        useText,
                        color: useTextColor,
                        fontSize: widget.fontSize ?? LStyle.fontSize,
                        lineHeight: 1.3,
                      ),
              ),
            ),
          )
      ],
    );
  }
}
